//
//  DPTabBarController.m
//  DP
//
//  Created by xy_ios on 15/6/9.
//  Copyright (c) 2015年 dp. All rights reserved.
//

#import "BaseTabBarController.h"
#import "BaseNavigationController.h"
//#import "HomeViewController.h"
#import "NewHomeViewController.h"
//#import "ShowViewController.h"
#import "MessageViewController.h"
#import "NewMessageViewController.h"
#import "ChatListViewController.h"

#import "MyViewController.h"

#define TabBarItem_FontSize 10.0
@interface BaseTabBarController ()

@end

@implementation BaseTabBarController

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initControllers];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.tabBar setBackgroundColor:[UIColor redColor]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)initControllers {
    UIViewController *homePageVC = [self createController:[NewHomeViewController new] normalImg:@"icon-1-1" selectImg:@"icon-1-2" title:@"首页"];
//    UIViewController *communityVC = [self createController:[NewMessageViewController new] normalImg:@"icon-2-1" selectImg:@"icon-2-2" title:@"聊天"];
    UIViewController *communityVC = [self createController:[MessageViewController new] normalImg:@"icon-2-1" selectImg:@"icon-2-2" title:@"聊天"];
//    UIViewController *communityVC = [self createController:[ChatListViewController new] normalImg:@"icon-2-1" selectImg:@"icon-2-2" title:@"聊天"];

    UIViewController *personalVC = [self createController:[MyViewController new] normalImg:@"icon-3-1" selectImg:@"icon-3-2" title:@"我的"];

    self.viewControllers = @[homePageVC,communityVC,personalVC];

    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    [UITabBar appearance].translucent = NO;
    
//    CGRect rect = CGRectMake(0, 0, App_Frame_Width, BHLine_Height);
//    UIGraphicsBeginImageContext(rect.size);
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSetFillColorWithColor(context, Dividing_Line_Color.CGColor);
//    CGContextFillRect(context, rect);
//    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    [self.tabBar setShadowImage:img];
//    [self.tabBar setBackgroundImage:[[UIImage alloc]init]];
}

- (UIViewController *)createController:(UIViewController *)controller normalImg:(NSString *)normalImg selectImg:(NSString *)selectImg title:(NSString *)title {
    UINavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:controller];
    nav.tabBarItem = [BaseTabBarController createTabbarItemTitle:title ForImage:[UIImage imageNamed:normalImg] ForSelectImage:[UIImage imageNamed:selectImg]];
    [nav.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:TabBar_NormalColor,NSForegroundColorAttributeName,[UIFont systemFontOfSize:TabBarItem_FontSize],NSFontAttributeName, nil]forState:UIControlStateNormal];
    [nav.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:TabBar_SelectColor,NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    
    return nav;
}

#pragma mark - class method
+ (UITabBarItem *)createTabbarItemTitle:(NSString *)title ForImage:(UIImage *)image {
    UIImage *oImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:title image:oImage tag:0];
    
    return item;
}

+ (UITabBarItem *)createTabbarItemTitle:(NSString *)title ForImage:(UIImage *)image ForSelectImage:(UIImage *)sImage {
    UIImage *oImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *osImage = [sImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:title image:oImage selectedImage:osImage];
//    item.badgeValue = @"3";
    if (@available(iOS 10.0, *)) {
        [item setBadgeColor:[UIColor colorWithHexString:@"FF4A3E"]];
    } else {
        // Fallback on earlier versions
    }
    item.titlePositionAdjustment = UIOffsetMake(0, - 3);
    if (title == nil) {
        CGFloat offset = 5.0;
        item.imageInsets = UIEdgeInsetsMake(offset, 0, -offset, 0);
    }
    return item;
}

@end
